/*
 * @Author: CodeHare
 * @Date: 2023-11-22 18:02:22
 * @LastEditors: yangni 920556271@qq.com
 * @LastEditTime: 2024-04-26 17:08:34
 * @Description: 新增报障信息
 */
import 'package:flutter/foundation.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:native_dart_package/loading/loading_util.dart';
import 'package:reactive_forms/reactive_forms.dart';
import 'package:yt_devops/configs.dart';
import 'package:yt_devops/pages/tasks/my_fault_report/create_fault_report/create_fault_report_model.dart';
import 'package:yt_devops/pages/tasks/my_fault_report/fault_report_list_controller.dart';
import 'package:yt_devops/pages/site/site_list/site_list_model.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/services/common/common_services.dart';
import 'package:yt_devops/services/site/site_services.dart';
import 'package:yt_devops/services/task/task_services.dart';

class CreateFaultReportController extends GetxController {
  CreateFaultReportModel model = CreateFaultReportModel();
  CommonServices commonServices = Get.find();
  TaskServices services = Get.find();
  SiteServices siteServices = SiteServices();
  FaultReportListController faultReportListController = Get.find();
  late FormGroup form;

  FormGroup buildForm() => fb.group(<String, Object>{
        'site': FormControl<SiteModel>(
            value: null, validators: [Validators.required]),
        'name':
            FormControl<String>(value: '', validators: [Validators.required]),
        'content': FormControl<String>(validators: [Validators.required]),
        'descPicUrl': FormControl<List<String>>(value: []),
      });

  int? id;

  Future<void> commit(FormGroup form)
  // =>
  //     Configs.httpClient.requestWithLoading(() async
      async {
        if (!form.valid) {
          return;
        }

        var params = form.rawValue;
        params.putIfAbsent('siteId', () => form.control('site').value.siteId);
        params['descPicUrl'] = form.control('descPicUrl').value.join(',');
        params.remove('site');
        showLoading();
        await services.createObstacleReport(params);
        dismissLoading();
        EasyLoading.showSuccess(ResKey.operateSuccessTip.tr).then((value) {
          Get.back(result: true);
        });
      }
}
